/* mcu.c */

#include "Mcu.h"

 /* Actual Bus frequency value */
u32 mcal_mcu_busFrequency;


/**************************************************************************************
* - Function      Mcu_Init
* - Description   This service initializes the MCU driver
* - Author        Equipo 1
* - Param         Mcu_ConfigType* ConfigPtr
* - Return        void
****************************************************************************************/

void Mcu_Init(void) {
  
          /* already initialized in Startup code */
}

/**************************************************************************************
* - Function      Mcu_InitClock
* - Description   This service initializes the PLL and other MCU specific clock options.
* - Author        Equipo 1
* - Param         void
* - Return        void
****************************************************************************************/

void Mcu_InitClock(void) {
  
        /* Disable PLL */
        PLLCTL_PLLON = 0u;         
           
        /* Set PLL synthesizer register */
        SYNR_SYNDIV = CNF_SYNR_VALUE;         
        /* Set PLL divider register */  
        REFDV_REFDIV = CNF_REFDV_VALUE;         
        /* Configure PLL filter for optimal stability and lock time */
        REFDV_REFFRQ = CNF_REFFRQ_VALUE;        
        /* Set Postdiv value,  */
        POSTDIV_POSTDIV = CNF_POSTDIV_VALUE;                
        /* Configure VCO gain for optimal stability and lock time */
        SYNR_VCOFRQ = CNF_VCOFRQ_VALUE;         
        /* Clock Monitor Enable. This is required to enter Self Clock Mode */
        PLLCTL_CME = 1u;                     
        /* Self Clock Mode Enable, If CME & SCME are both asserted and the Clock Monitor
        in the oscillator block detects a loss of clock, Self Clock Mode will be entred */
        PLLCTL_SCME = 1u;                    
        /* Clear Self Clock Mode Interrupt Flag */                                                                          
        CRGFLG_SCMIF = 1u;                   
        /* Interrupt will be requested whenever SCMIF(Self Clock Mode Interrupt Flag) is set */ 
        CRGINT_SCMIE = 1u;                    
    
        /* PLL enabled */
        PLLCTL_PLLON = 1u;                                 
        /* Wait until the PLL is within the desired frequency */
        while(!CRGFLG_LOCK);
        /* Clear IPLL Lock Interrupt Flag */
        CRGFLG_LOCKIF = 1u;                  
        /* Interrupt will be requested whenever LOCKIF(IPLL Lock Interrupt Flag)is set */        
        CRGINT_LOCKIE = 1u;
         
}


/**************************************************************************************
* - Function      Mcu_DistributePllClock
* - Description   This service activates the PLL clock to the MCU clock distribution
* - Author        Equipo 1
* - Param         void
* - Return        void
****************************************************************************************/

void Mcu_DistributePllClock(void) {

      /* Select PLL as clock source */
        CLKSEL_PLLSEL = 1u;
        
        /* update bus frequency variable with PLL frequency value */
        mcal_mcu_busFrequency = CNF_ACTUAL_BUS_FREQ;
  
}



